package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.itheima.dao.AuthDao;
import com.itheima.dao.CategoryMenuMapper;
import com.itheima.pojo.Admin;
import com.itheima.pojo.CategoryMenu;
import com.itheima.pojo.Role;
import com.itheima.service.AuthService;
import com.itheima.utils.Result;
import com.itheima.vo.Assignbody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthDao authDao;
    @Autowired
    private CategoryMenuMapper categoryMenuMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void register(Assignbody assignbody) {
        String jsonString = JSON.toJSONString(assignbody);
        Admin admin = JSON.parseObject(jsonString, Admin.class);

        String passWord = admin.getPassWord();
        String gensalt = BCrypt.gensalt();
        String password = BCrypt.hashpw(passWord, gensalt);
        System.out.println("1111111="+password);
        admin.setPassWord(password);

        authDao.insert(admin);
    }

    @Override
    public Result login(String userName, String passWord) {

        Admin admin = new Admin();
        admin.setUserName(userName);

        List<Admin> select = authDao.select(admin);
        System.out.println("查询出来的集合="+select);
        if (select==null || select.size()==0) {
            return new Result("error","没有此用户");
        }


        for (Admin admin1 : select) {
            if (!BCrypt.checkpw(passWord,admin1.getPassWord())){
                return new Result("error","旧密码不正确");
            }
        }
        return new Result();
    }

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public Admin login(String username, String password, String remoteAddr) {
        Admin admin = new Admin();
        admin.setUserName(username);
        admin.setPassWord(password);
        List<Admin> adminList = authDao.select(admin);
        return adminList.get(0);
        /*Example example=new Example(Admin.class);
        example.createCriteria().andEqualTo("userName",username);
        List<Admin> admins = authMapper.selectByExample(example);

        if (admins==null||admins.size()==0){
            return null;
        }

        boolean b = false;
        try {
           // b = BCrypt.checkpw(password,admins.get(0).getPassWord());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (b){
            Admin admin = admins.get(0);
            admin.setLastLoginIp(remoteAddr);
            authMapper.updateByPrimaryKeySelective(admin);
        }

        return b;*/
    }


    /**
     * 根据管理员名称获取管理列表
     * @param username
     * @return
     */
    @Override
    public Map<String, List<CategoryMenu>> getMenu(String username) {
        Role role = authDao.getCategoryMenuUids(username);
        String categoryMenuUids = role.getCategoryMenuUids();
        System.out.println(categoryMenuUids);
        String s = categoryMenuUids.replaceAll("\\[", "")
                .replaceAll("\\]", "")
                .replaceAll("\"", "");
        String[] split = s.split(",");
        List<String> list = Arrays.asList(split);

        List<CategoryMenu>parentList=new ArrayList<>();
        List<CategoryMenu>buttonList=new ArrayList<>();
        List<CategoryMenu>sonList=new ArrayList<>();
        for (String uid : list) {
            CategoryMenu categoryMenu = categoryMenuMapper.selectByPrimaryKey(uid);
            if (categoryMenu.getMenuLevel()==1) {
                parentList.add(categoryMenu);
            }else if(categoryMenu.getMenuLevel()==2){
                sonList.add(categoryMenu);
            }else if(categoryMenu.getMenuType()==1){
                buttonList.add(categoryMenu);
            }
        }
        Map<String, List<CategoryMenu>> map=new HashMap();
        map.put("parentList",parentList);
        map.put("buttonList",buttonList);
        map.put("sonList",sonList);
        return map;
    }

    /**
     * 获取当前用户信息
     * @param username
     * @return
     */
    @Override
    public Map<String, Object> getInfo(String username) {
        Role role = authDao.getCategoryMenuUids(username);
        Admin admin = new Admin();
        admin.setUserName(username);
        Admin admin1 = authDao.selectOne(admin);

        Map<String,Object> map=new HashMap<>();
        List<Role>roles=new ArrayList<>();
        roles.add(role);
        map.put("roles",roles);
        map.put("avatar",admin1.getAvatar());
        return map;
    }

    @Override
    public void updatePassWord(String userName,String newPpassWord) {
        Admin admin = new Admin();
        admin.setUserName(userName);

        List<Admin> select = authDao.select(admin);
        for (Admin admin1 : select) {
            admin.setUid(admin1.getUid());
        }

        String password = BCrypt.hashpw(newPpassWord, BCrypt.gensalt());
        admin.setPassWord(password);

        authDao.updateByPrimaryKeySelective(admin);
    }

    @Override
    public boolean findByUserNnme(String userName) {
        Admin admin = new Admin();
        admin.setUserName(userName);

        List<Admin> adminList = authDao.select(admin);
        if (adminList==null ||  adminList.size()==0) {
            return false;
        }

        return true;
    }

    @Override
    public void logout(String accessToken) {
        redisTemplate.delete(accessToken);
    }
}
